package org.watchrecords.pages.statistics;

import java.text.DateFormat;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.watchrecords.dbo.CellRecord;
import org.watchrecords.dbo.Offender;
import org.watchrecords.elements.BasicForm;
import org.watchrecords.managers.CellLogManager;
import org.watchrecords.managers.OffenderManager;
import org.watchrecords.pages.BasicPage;
import org.watchrecords.pages.crime.ViewOffenderPage;
import org.watchrecords.util.FormatHelper;

import com.google.inject.Inject;

public class CellLogPage extends BasicPage {
    @Inject private OffenderManager offenderManager;
    @Inject private CellLogManager manager;
    
    public CellLogPage() {
        super(null);
        final DateFormat format = FormatHelper.getDateTimeFormat();
        
        add(new ListView<CellRecord>("recordList", manager.getCellRecords()) {
            private static final long serialVersionUID = 933172716441066883L;

            @Override
            protected void populateItem(ListItem<CellRecord> item) {
                final CellRecord record = item.getModelObject();
                item.add(new Label("type", record.isIncoming() ? "Incoming" : "Outgoing"));
                item.add(new Label("date", format.format(record.getRecordTime())));
                item.add(new Label("watchman", StringUtils.defaultString(record.getWatchman())));
                item.add(new Label("reason", StringUtils.defaultString(record.getCrimes())));
                item.add(new MultiLineLabel("possessions", StringUtils.defaultString(record.getPossessions())));
                item.add(new MultiLineLabel("notes", StringUtils.defaultString(record.getNotes())));
                
                item.add(new Link<Object>("offenderLink") {
                    private static final long serialVersionUID = 2887980413707206889L;

                    @Override
                    public void onClick() {
                        setResponsePage(ViewOffenderPage.class, new PageParameters("offenderId=" + record.getOffender().getId()));
                    }
                }.add(new Label("offenderName", record.getOffender().getFullName())));
            }
        }.setRenderBodyOnly(true));
        
        add(new FeedbackPanel("feedbackPanel"));
        add(new CellLogForm("form"));
    }

    private class CellLogForm extends BasicForm<CellRecord> {
        private static final long serialVersionUID = 6895732392223585124L;

        public CellLogForm(String id) {
            super(id, new CellRecord());
            
            RadioGroup<Boolean> type = new RadioGroup<Boolean>("incoming");
            type.add(new Radio<Boolean>("incoming", new Model<Boolean>(true)));
            type.add(new Radio<Boolean>("outgoing", new Model<Boolean>(false)));
            add(type);
            
            add(new DropDownChoice<Offender>("offender", offenderManager.getOffenders()));
            add(new DateTimeField("recordTime"));
            add(new TextField<String>("watchman"));
            add(new TextField<String>("crimes"));
            add(new TextArea<String>("possessions").setRequired(false));
            add(new TextArea<String>("notes").setRequired(false));
        }
        
        protected void doSubmit() {
            manager.saveCellRecord(getModelObject());
            setResponsePage(CellLogPage.class);
        }
    }
}
